import java.sql.*;
import java.util.Collection;

/**
 * Class that holds patient information Patient is a User This holds all of the
 * patient information that the patient can access It also enables the patient
 * to make an appointment
 */
public class Patient extends User {
	private PatientInfo patientInfo;
	private Billing billing;

	public Patient(String username, String password, Name name) {
		super(username, password, name, 1);
		patientInfo = null;
		billing = null;
	}

	/**
	 * Patient constructor that initializes patientInfo and billing
	 */
	public Patient() {
		patientInfo = null;
		billing = null;
	}

	/**
	 * Method that enables patient to request an appointment Returns true if
	 * selected day and time are not taken
	 * 
	 * @param appointment
	 *            - takes in appointment as an appintment
	 * @return boolean of whether appointment is available
	 */
	public boolean requestAppointment(Appointment appointment) {
		// if appointment is available
		// set appointment
		// if not, return message 'choose another
		// time or day'
		return true;
	}

	/**
	 * Enables user to view his/her information Past appointments, records,
	 * history, etc
	 * 
	 * @return patient information as patient info
	 */
	public PatientInfo getPatientInfo() {
		return patientInfo;
	}

	/**
	 * Enables user to change his/her personal info:address, phone number etc
	 * 
	 * @param info
	 *            in the form of patient info
	 */
	public void setPatientInfo(PatientInfo info) {
		this.patientInfo = info;
	}

	// CRUD functionality below

	// create
	public static boolean createPatient(String username, String encryptedPass,
			boolean suspended) {
		if ((username.length() == 0) || (encryptedPass.length() == 0)) {
			return false;
		}
		String sql = "INSERT INTO Users VALUES ('" + username + "','"
				+ encryptedPass + "',1," + suspended + ")";
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// read
	public static Patient readPatient(String username) {
		String foundPass = "";
		int foundAccess = 0;
		String sql = "SELECT * FROM Users WHERE username='" + username + "'";
		ResultSet rs = Database.selectQuery(sql);
		// should only be 1 record per username
		try {
			while (rs.next()) {
				foundPass = rs.getString("encrypted_password");
				foundAccess = rs.getInt("access_level");
			}
		} catch (Exception e) {
		}
		// get the name of the patient
		Name patientName = Name.readName(username);
		Patient foundPatient = new Patient(username, foundPass, patientName,
				foundAccess);
		return foundPatient;
	}

	// update
	public static boolean updatePatient(String username, String encryptedPass,
			boolean suspended) {
		int suspendValue;
		if (suspended)
		{
			suspendValue = 1;
		}
		else
		{
			suspendValue = 0;
		}
		String sql = "UPDATE Users SET encrypted_password='" + encryptedPass
				+ "',  suspended='" + suspendValue + "' WHERE username='"
				+ username + "'";
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// delete
	public static boolean deletePatient(String username) {
		String sql = "DELETE FROM Users WHERE username='" + username + "'";
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
